﻿using GameEntitySystem;
using Game;
using TemplatesDatabase;
using System.Collections.Generic;

namespace Mekiasm.Buff
{
    public class ComponentBuff : Component, IUpdateable
    {
        public ComponentBody Body;
        public ComponentLevel Level;
        public ComponentHealth Health;
        public ComponentGui Gui;
        public List<Buff> Buffs = new List<Buff>();
        public List<Buff> ToRemove = new List<Buff>();
        public UpdateOrder UpdateOrder => UpdateOrder.Default;
        public SubsystemGameInfo GameInfo;
        public ExtraGui.ComponentExtraGui Extra;
        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            Body = Entity.FindComponent<ComponentBody>();
            Level = Entity.FindComponent<ComponentLevel>();
            Health = Entity.FindComponent<ComponentHealth>();
            GameInfo = Project.FindSubsystem<SubsystemGameInfo>();
            Gui = Entity.FindComponent<ComponentGui>();
            Extra = Entity.FindComponent<ExtraGui.ComponentExtraGui>();

            foreach (ValuesDictionary i in valuesDictionary.GetValue<ValuesDictionary>("BuffList").Values)
            {
                Buff buff = new Buff();
                buff.id = i.GetValue<int>("i");
                buff.startGameTime = i.GetValue<int>("s");
                buff.endGameTime = i.GetValue<int>("e");
                buff.startValue = i.GetValue<double>("b");
                buff.factor = i.GetValue<double>("f");
                buff.isExec = false;
                buff.GameInfo = GameInfo;
            }
            base.Load(valuesDictionary, idToEntityMap);
        }
        /// <summary>
        /// 添加Buff
        /// </summary>
        /// <param name="id">buff的ID</param>
        /// <param name="buffTime">buff持续的游戏时间</param>
        public void AddBuff(int id, double buffTime)
        {
            Buff buff = new Buff();
            buff.id = id;
            buff.desc = MekBuffDesc.Get(id);
            buff.startGameTime = GameInfo.TotalElapsedGameTime;
            buff.endGameTime = buff.startGameTime + buffTime;
            buff.GameInfo = GameInfo;
            var b = Buffs.Find(w => w.id == id);
            if (b != null)
            {
                //刷新时间
                b.startGameTime = buff.startGameTime;
                b.endGameTime = buff.endGameTime;
            }
            else
            {
                Buffs.Add(buff);
            }
        }
        /// <summary>
        /// 第一次生效
        /// </summary>
        /// <param name="buff"></param>
        public void onStartBuff(Buff buff)
        {
            switch (buff.id)
            {
                case 3://增加移速
                    buff.startValue = Level.SpeedFactor;
                    break;
                case 8://掉落伤害免疫
                    buff.startValue = Health.FallResilience;
                    break;
                case 25://体力恢复加快
                    
                    break;
            }
        }
        /// <summary>
        /// 更新效果
        /// </summary>
        /// <param name="buff"></param>
        /// <param name="dt"></param>
        public void onUpdateBuff(Buff buff,float dt)
        {
            switch (buff.id)
            {
                case 2://生命恢复速度提升
                    if (Health != null)
                    {
                        Health.Health += dt * 0.01f;
                    }
                    break;
                case 3://移速提升
                    Level.SpeedFactor = (float)(buff.startValue * buff.factor);
                    break;
                case 5://防御提升
                    
                    break;
                case 8://掉落伤害免疫
                    Health.FallResilience = (float)(buff.startValue * buff.factor);
                    break;
                case 25://体力恢复加快
                    break;
            }
        }
        /// <summary>
        /// 效果结束
        /// </summary>
        /// <param name="buff"></param>
        public void onRemoveBuff(Buff buff)
        {
            switch (buff.id)
            {
                case 3://移除移速
                    Level.SpeedFactor = (float)buff.startValue;
                    break;
                case 8://掉落伤害免疫
                    Health.FallResilience = (float)buff.startValue;
                    break;
                case 25://体力恢复加快
                    
                    break;

            }
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            ValuesDictionary pairs = new ValuesDictionary();
            int i = 0;
            foreach (Buff buff in Buffs)
            {
                ValuesDictionary pairs1 = new ValuesDictionary();
                pairs.SetValue(i++.ToString(),pairs1);
                pairs1.SetValue("s",buff.startGameTime);
                pairs1.SetValue("e", buff.endGameTime);
                pairs1.SetValue("i", buff.id);
                pairs1.SetValue("b",buff.startValue);
                pairs1.SetValue("f",buff.factor);

            }
            valuesDictionary.SetValue("BuffList",pairs);
            base.Save(valuesDictionary, entityToIdMap);
        }

        public void Update(float dt)
        {
            foreach (var b in Buffs)
            {
                if (!b.isExec)
                {
                    onStartBuff(b);
                    b.isExec = true;
                }
                if (b.endGameTime < GameInfo.TotalElapsedGameTime) ToRemove.Add(b);
                onUpdateBuff(b,dt);
            }
            foreach (var r in ToRemove)
            {
                Buffs.Remove(r);
                Extra.BuffView.Children.Remove(r.View);
                onRemoveBuff(r);
            }
            ToRemove.Clear();
        }
    }
}
